package task

import (
	"pids-cloud-server/entity"
	"pids-cloud-server/logging"
	"pids-cloud-server/utils"
	"time"
)

func ClearLogTask() {
	logging.Info("----------定时清空历史数据任务开始----------")

	// 获取数据库连接
	db := utils.GetDB()
	if db == nil {
		logging.Error("数据库连接失败，跳过清空历史数据任务")
		return
	}

	nowTime := time.Now()
	// 计算前一天的时间
	thrityDayBefore := nowTime.AddDate(0, 0, -15)
	db.Where("create_time < ?", thrityDayBefore).Delete(&entity.CpuState{})
	db.Where("create_time < ?", thrityDayBefore).Delete(&entity.DeskState{})
	db.Where("create_time < ?", thrityDayBefore).Delete(&entity.MemState{})
	logging.Info("----------定时清空历史数据任务结束----------")
}
